//
//  TTMusicMusicGuanCollectionCell.m
//  TTMusic
//
//  Created by 马金晖 on 16/3/31.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "TTMusicMusicGuanCollectionCell.h"
#import "HJCarouselViewLayout.h"
#import "TTMusicMusicGuanSecondCell.h"
#import "TTMusicURLManager.h"
#import "TTMusicAFNetworkTool.h"
#import "UIImageView+WebCache.h"


#define WIDTH         self.bounds.size.width


@implementation TTMusicMusicGuanCollectionCell


-(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame: frame];
    
    if (self) {
        [self getRequestData];
        
    }
    return self;
}


#pragma mark - 数据请求
-(void)CreatCollection {
    HJCarouselViewLayout *layout=[[HJCarouselViewLayout alloc] initWithAnim:(HJCarouselAnimLinear)];
    layout.itemSize=CGSizeMake(WIDTH*0.9, WIDTH*0.9);
    layout.visibleCount = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView registerClass:[TTMusicMusicGuanSecondCell class] forCellWithReuseIdentifier:@"TTMusicMusicGuanSecondCellIdentifier"];
    
    [self addSubview:_collectionView];
    
}

#pragma mark - 数据请求
-(void)getRequestData {
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    
    [TTMusicAFNetworkTool GETNetWithUrl:@"http://api.dongting.com/misc/album/new?size=10&page=1" body:nil headerFile:nil response:TTMusicJSON success:^(id result) {
        
        NSArray *data = result[@"data"];
        
        for (NSDictionary * dic in data) {
            self.tuijianFirstModel = [[TuiJianFirstModel alloc]initWithDataSource:dic];
            
            
            [self.dataSource addObject:self.tuijianFirstModel];
        }
        
        [self CreatCollection];
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TTMusicMusicGuanSecondCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"TTMusicMusicGuanSecondCellIdentifier" forIndexPath:indexPath];
    
    
    cell.tuijianFirstModel = self.dataSource[indexPath.item];
    
    
    
    return cell;
}


- (NSIndexPath *)curIndexPath {
    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    NSIndexPath *curIndexPath = nil;
    NSInteger curzIndex = 0;
    for (NSIndexPath *path in indexPaths.objectEnumerator) {
        UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:path];
        if (!curIndexPath) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
            continue;
        }
        if (attributes.zIndex > curzIndex) {
            curIndexPath = path;
            curzIndex = attributes.zIndex;
        }
    }
    return curIndexPath;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *curIndexPath = [self curIndexPath];
    if (indexPath.row == curIndexPath.row) {
        return YES;
    }
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    return NO;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.delegate pushNextVCWithDataSource:self.dataSource WithIndex:indexPath.row];
    
    
    
}




@end
